<?php
namespace app\api\controller;
use app\model\Sign as SignModel;
use app\model\Token as TokenModel;
use app\model\Integral as IntegralModel;
use think\Db;


class Sign extends Base
{   
    
    /**
     * @Author   CCH
     * @DateTime 2020-05-23T12:10:10+0800
     * @return   数据列表
     */
    public function index(){
        $ulogin = $this->login_user;
        $map_params = [
            
        ];
        $map = getSearchParam($map_params);
        $map[] = ['user_id','=',$ulogin['user_id']];

        $model = new SignModel();
        $page_param = ['page'=>input('page_index',1),'list_rows'=>input('page_size',10)];
        $list = $model->where($map)->order('id desc')->paginate($page_param)->toarray();
        foreach ($list['data'] as $k => $vo) {
            
        }
        return apiResult('2000','获取成功',$list);
    }

    public function save(){
        $model = new SignModel();
        $ulogin = $this->login_user;
        if ( $model->where([
            ['user_id','=',$ulogin['user_id']],
            ['ctime','=',date('Y-m-d')]
        ])->count() > 0 ) {
            return apiResult('5000','今日已签到，明日再来');
        }

        $save_data = [
            'user_id'=>$ulogin['user_id'],
            'ctime'=>date('Y-m-d'),
            'integral'=>Db::name('config')->where('key','SIGN_INTEGRAL')->value('value')
        ];
        $model->insert($save_data);

        IntegralModel::saveData([
            'user_id'=>$ulogin['user_id'],
            'remark'=>'每日签到',
            'amount'=>$save_data['integral'],
            'type'=>2
        ]);

        return apiResult('2000','签到成功');
    }



}
